package com.yiren.dbaa.module.project_edit.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.bean.GradeStandardEditBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/14 14:43
 * @description: 评分修改dialog
 */
public class GradeStandardEditDialog {

    public static int selectIndex = 0;
    public static int selectPoints = 100;

    public static void show(Context context, int points, String content, String contenjx, CommonCallBack callBack) {
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setCancelable(false);
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        params.width = (metric.widthPixels * 6) / 10;
        params.height = (metric.heightPixels * 8) / 8;
        dialog.getWindow().setAttributes(params);
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_grade_standard);
        Button btnSure = (Button) window.findViewById(R.id.bt_dialog_ok);
        Button btnCancel = window.findViewById(R.id.bt_dialog_cancel);
        EditText et_conten = (EditText) window.findViewById(R.id.et_grade_content);
        EditText et_contenjx = (EditText) window.findViewById(R.id.et_grade_content_jx);
        Spinner sp_points = (Spinner) window.findViewById(R.id.sp_grade_points);
        et_conten.setText(content);
        et_contenjx.setText(contenjx);
        List<String> list = new ArrayList<>();
        list.add("100分");
        list.add("10分");
        list.add("5分");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.item_spinner, list);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        sp_points.setAdapter(adapter);
        if (points == 100) {
            selectIndex = 0;
        } else if (points == 10) {
            selectIndex = 1;
        } else {
            selectIndex = 2;
        }
        sp_points.setSelection(selectIndex);
        sp_points.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    selectPoints = 100;
                } else if (position == 1) {
                    selectPoints = 10;
                } else {
                    selectPoints = 5;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.success(new GradeStandardEditBean(et_conten.getText().toString(), et_contenjx.getText().toString(), selectPoints));
                dialog.dismiss();
            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });


    }

}
